<?php

/**
 * Form for account apply, allows to create profile (sfGuardUserProfile) and account (sfGuardUser)
 * @author fizyk
 */
class mySfApplyApplyForm extends sfApplyApplyForm
{
  
    public function configure()
    {
         parent::configure();
         
         unset($this['user_id'], $this['validate'], $this['validate_at'],
                $this['created_at'], $this['updated_at'], $this['email_new'],
                 $this['email_hash'], $this['facebook_uid']);

        //Setting username widget
        $this->setWidget( 'username',
                new sfWidgetFormInput( array(), array( 'maxlength' => 16 ) ) );
       // $this->widgetSchema->moveField('username', sfWidgetFormSchema::FIRST);

        //Setting password widgets
        $this->setWidget( 'password',
                new sfWidgetFormInputPassword( array(), array('maxlength' => 128) ) );
       // $this->widgetSchema->moveField('password', sfWidgetFormSchema::AFTER, 'username');

        $this->setWidget('password2',
                new sfWidgetFormInputPassword( array(), array('maxlength' => 128) ) );
       // $this->widgetSchema->moveField('password2', sfWidgetFormSchema::AFTER, 'password');

        //Settings for email fields
        $email = $this->getWidget('email');
        $class = get_class($email);
        $this->setWidget('email2',
                new $class( array(), array('maxlength' => $email->getAttribute('maxlength') )
        ) );
        //$this->widgetSchema->moveField( 'email2', sfWidgetFormSchema::AFTER, 'email' );
         //Firstname and lastname
        $this->setWidget( 'firstname', new sfWidgetFormInputText( array(), array( 'maxlength' => 30 ) ) );
        $this->setWidget( 'lastname', new sfWidgetFormInputText( array(), array( 'maxlength' => 70 ) ) );

         $this->setWidget('birthday',
                 new sfWidgetFormJQueryDate(
                         array('image'=> '/images/icon/calendar.gif',
                               'culture' => sfContext::getInstance()->getUser()->getCulture())));
                                                         
        $this->widgetSchema->setLabels( array(
            'username' => 'Username',
            'password' => 'Password',
            'password2' => 'Confirm password',
            'email' => 'Email address',
            'email2' => 'Confirm email',
            'firstname' => 'First Name',
            'lastname' => 'Last name',
            'birthday' => 'Birthday'
        ) );
        if ($this->isCaptchaEnabled() )
        {
            $this->addCaptcha();
        }
        $this->widgetSchema->setNameFormat('sfApplyApply[%s]');
        $this->widgetSchema->setFormFormatterName('list');

        // Validators
        $this->setValidator ( 'username', new sfValidatorApplyUsername() );
        $this->setValidator( 'password', new sfValidatorApplyPassword() );
        $this->setValidator( 'password2', new sfValidatorApplyPassword() );

        $this->setValidator('email', new sfValidatorAnd(array(
            new sfValidatorEmail(array('required' => true, 'trim' => true)),
            new sfValidatorString(array('required' => true, 'max_length' => 80)),
            new sfValidatorDoctrineUnique(
                    array( 'model' => 'sfGuardUserProfile', 'column' => 'email'),
                    array('invalid' => 'An account with that email address already exists. If you have forgotten your password, click "cancel", then "Reset My Password."') )
        )));

        $this->setValidator('email2', new sfValidatorEmail(
                array( 'required' => true, 'trim' => true )));


        $this->setValidator('firstname', new sfValidatorApplyFirstname() );

        $this->setValidator('lastname', new sfValidatorApplyLastname() );

        $schema = $this->validatorSchema;

        // Hey Fabien, adding more postvalidators is kinda verbose!
        $postValidator = $schema->getPostValidator();

        $postValidators = array(
            new sfValidatorSchemaCompare( 'password', sfValidatorSchemaCompare::EQUAL,
                    'password2', array(), array('invalid' => 'The passwords did not match.') ),
            new sfValidatorSchemaCompare( 'email', sfValidatorSchemaCompare::EQUAL,
                    'email2', array(), array('invalid' => 'The email addresses did not match.') ) );

        if( $postValidator )
        {
            $postValidators[] = $postValidator;
        }
        $this->validatorSchema->setPostValidator( new sfValidatorAnd($postValidators) );

    }

    /*
    public function doSave($con = null)
    {
        $user = new sfGuardUser();
        $user->setUsername($this->getValue('username'));
        $user->setPassword($this->getValue('password'));
        $user->setFirstName($this->getValue('firstname'));
        $user->setLastName($this->getValue('lastname'));
        $user->setEmailAddress($this->getValue('email'));
        // They must confirm their account first
        $user->setIsActive(false);
        $user->save();
        $this->userId = $user->getId();

        return parent::doSave($con);
    }
    */

}

